package com.online_video.dao.impl;

import com.online_video.dao.BaseDao;
import com.online_video.dao.UserDao;
import com.online_video.entity.User;

import java.util.List;

/**
 * @author zhenghouwen
 * @create 2022-03-14 11:05
 */
public class UserDaoImpl extends BaseDao implements UserDao {

    @Override
    public int insert(User user) {
        String sql = "INSERT INTO `T_user` (`role`, `username`, `password`, `nickname`, `telephone`, `email`, `gender`, `introduce`, `headportrait`, `isactive`, `isvip`) VALUES (?,?,?,?,?,?,?,?,?,?,?)";
        return super.update(sql,user.getRole(),user.getUsername(),user.getPassword(),user.getNickname(),user.getTelephone(),user.getEmail(),user.getGender(),user.getIntroduce(),user.getHeadportrait(),user.getIsactive(),user.getIsvip());
    }

    @Override
    public int update(User user) {
        String sql = " UPDATE `T_user` SET `role`=?, `username`=?, `password`=?, `nickname`=?, `telephone`=?, `email`=?, `gender`=?, `introduce`=?, `headportrait`=?, `isactive`=?, `isvip`=?  WHERE `id`=?";
        return super.update(sql,user.getRole(),user.getUsername(),user.getPassword(),user.getNickname(),user.getTelephone(),user.getEmail(),user.getGender(),user.getIntroduce(),user.getHeadportrait(),user.getIsactive(),user.getIsvip(),user.getId());
    }

    @Override
    public int delete(Integer userId) {
        String sql = "DELETE FROM `T_user` WHERE `id` = ?";
        return super.update(sql,userId);
    }

    @Override
    public User queryByUsername(String username) {
        String sql = "SELECT * FROM `T_user` WHERE `username` = ?";
        return super.queryForOne(User.class, sql, username);
    }

    @Override
    public User queryByUsernameAndPassword(User user) {
        String sql = "SELECT * FROM `T_user` WHERE `username` = ? AND `password` = ?";
        return super.queryForOne(User.class,sql,user.getUsername(),user.getPassword());
    }

    @Override
    public List<User> queryAll() {
        return null;
    }

    @Override
    public int updatePwd(String pwd,Integer userId) {
        String sql = "UPDATE `T_user` SET `password` = ? WHERE `id` = ?";
        return super.update(sql, pwd, userId);
    }

    public User queryByU_Id(Integer u_id) {
        String sql = "SELECT * FROM `T_user` where id = ?";
        return super.queryForOne(User.class,sql,u_id);
    }

    @Override
    public Integer queryForPageTotalCount() {
        String sql = " SELECT count(1)From `T_user`";
        Number count = (Number)queryForSingleValue(sql);
        return count.intValue();
    }

    @Override
    public List<User> queryForPageItems(int begin, int pageSize) {
        String sql = "SELECT * FROM `T_user` LIMIT ?,? ";
        return super.queryForList(User.class,sql,begin,pageSize);
    }


}
